var appInstance = getApp();
Component({
  /**
   * 组件的属性列表
   */
  options: {
    addGlobalClass: true
  },

  properties: {

    option_panel:Object,
    "product_id":Number,
    "product_image":String,
    "price":Number,
    "specification":{
      type:Object,
    },
    "spec_object":Array,
    "goto":String
  },

  /**
   * 组件的初始数据
   */
  data: {

    show:0,
    local_notice:0,
    spec_checked:{},
    matched_option:null,
    goods_num:1,
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {
      console.log( 'attached' );
    },
    moved: function () {
      console.log( 'moved' );
    },
    detached: function () {
      console.log( 'detached' );
    },
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached: function () { }, // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready: function() {
    console.log( 'ready' );

    var t = this;
    setTimeout(function(){
      console.log( t.data['specification'] );
    },3000);

  },

  observers:{


    'option_panel.notice':function( after ){

      var show = after > this.data.local_notice ? 1: 0;
      this.setData({
        show:show,
        local_notice:after
      });

    }
  },
  /**
   * 组件的方法列表
   */
  methods: {


    change_num:function( event ){

      var t = this;


      var dataset = event.target.dataset;
      var product = dataset.product;

      if( dataset.type == '-' ){
        this.data.goods_num --;
      }else{
        this.data.goods_num ++;
      }
      this.data.goods_num  = this.data.goods_num <= 0 ? 1 : this.data.goods_num;

      // todo 刷新数据
      this.setData({
        goods_num:this.data.goods_num
      });
    },

    check_spec_value:function ( e ) {

      var dataset = e.target.dataset;
      var spec = dataset.spec;
      var value = dataset.value;

      if(this.data.spec_checked[ spec.name ] == value.name){
        // this.$set(this.spec_checked, spec.name, '');
        this.data.spec_checked[ spec.name ] = '';
      }else{
        this.data.spec_checked[ spec.name ] = value.name;
        // this.$set(this.spec_checked, spec.name, value.name);
      }

      this.setData({
        spec_checked:this.data.spec_checked
      });
      this.match_option();

    },

    match_option:function(){

      var t = this;
      t.setData({
        matched_option: null
      });
      this.data.spec_object.forEach(function( item ){

        var attr_arr = item.attr.split(',');
        var matched = 1;
        attr_arr.forEach(function( attr ){

          var key = attr.split(':')[0];
          var v = attr.split(':')[1];
          if( t.data.spec_checked[ key ] != v){
            matched = 0;
          }
        });

        if( matched ){
          // t.data.matched_option = item;

          t.setData({
            matched_option: item
          });

        }

      });

    },


    done:function( e ){

      var t = this;

      this.match_option();

      if( !this.data.matched_option ){

        wx.showToast({
          title: '请选中规格',
          icon: 'success',
          duration: 600
        });
        return;
      }

      if( this.data.option_panel.goto == 'add_to_cart' ){
        this.add_to_cart()
      }
      if( this.data.option_panel.goto == 'buy' ){
        this.buy()
      }
      if( this.data.option_panel.goto == 'buy_pindan' ){
        this.buy()
      }

    },

    add_to_cart:function(){


      var url= appInstance.globalData.api_path+'&r=cart.add';

      var data = {
        product_id:this.data.product_id,
        option_id:this.data.matched_option.id,
        number: this.data.goods_num,
      };
      var t = this;
      appInstance.request( url, data, 'GET', function( res ){

        if( !res.data.status ){
          wx.showToast({
            title: '购物车添加成功',
            icon: 'success',
            duration: 800
          });

          t.setData({
            show:0
          });

          //todo 更新购物车 状态
          // t.cartStatus();
          var myEventDetail = {} // detail对象，提供给事件监听函数
          var myEventOption = {} // 触发事件的选项
          t.triggerEvent('poEvent', myEventDetail, myEventDetail);
        }



      });

    },

    buy:function(){

      var params = 'product_id='+this.data.product_id;
      if( this.data.matched_option ){
        params += '&option_id='+this.data.matched_option.id;
      }
      params += '&product_num='+this.data.goods_num;
      if( this.data.option_panel.goto == 'buy_pindan' ){
        params += '&is_pindan=1&pindan_order=' + this.data.option_panel.pindan_order;

      }
      wx.navigateTo({
        url:'/pages/checkout/checkout?' + params
      });
    }

  }
})
